﻿using System;
using System.Collections.Generic;
using System.Text;
using RS.DOM.Common;

namespace RS.DOM.Types
{
    public class ResultSet : Token
    {
        public ResultSet(Context context)
            : base(context)
        {
            base.TokenType = TokenType.Mutable;
            base.Result = new List<Token>();
        }

        public override Token Operation(Token token, OperationType type)
        {
            if (type == OperationType.Agregate)
            {
                return null;
            }
            else if (type == OperationType.Compare)
            {
                bool res = false;

                foreach (Token currToken in this.Result)
                {
                    if (currToken.TokenType == TokenType.Bool)
                        res |= ((Bool)currToken).Result;
                }

                if (res)
                    return token;
                else
                    return new Null(this.Context);
            }
            else if (type == OperationType.CompareInNot)
            {
                bool res = false;

                foreach (Token currToken in this.Result)
                {
                    if (currToken.TokenType == TokenType.Bool)
                        res |= ((Bool)currToken).Result;
                }

                if (res)
                    return new Null(this.Context);
                else
                    return token;
            }
            else
            {
                return base.Operation(token, type);
            }
        }

        public override string ToString()
        {
            return "ResultSet";
        }

        public List<Token> Result
        {
            get
            {
                return (List<Token>)base.Result;
            }
        }
    }
}
